// 获取扫普通链接二维码打开小程序传递给页面的q参数中携带的参数
export const getWxQuery = (key:string, scene:string) => {
	return getWxScene(key, decodeURIComponent(scene))
}
// 获取小程序或公众号的scene参数
export const getWxScene = (key:string, scene:string) => {
	const queryNameRegex = new RegExp(`${key}=([^&]*)(&|$)`)
	let queryNameMatch = scene.match(queryNameRegex)
	let params = null
	if (queryNameMatch) {
		params = queryNameMatch[1]
		let length = params.length - 1
		if (params[length] === '/') {
			params = params.slice(0, length)
		}
	}
	return params
}
// 对使用encodeURIComponent编码的url参数进行解码，转换为对象
// 也可用于解码小程序或公众号的scene参数
export const sceneParamsToObj = (url:string) => {
	let obj:Record<string, string> = {}
	if (url) {
		let params = decodeURIComponent(url).split('&')
		params.forEach(item => (obj[item.split('=')[0]] = item.split('=')[1]))
	}
	return obj
}